package com.maven.front.controller.eduStatisticsDay;

import com.maven.front.bean.EduStatisticsDay;
import com.maven.front.bean.Result;
import com.maven.front.service.edustatisticsDay.EduStatisticsDayService;
import com.maven.front.util.DateUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.*;

@Slf4j
@CrossOrigin
@RestController
@RequestMapping("admin/eduStatistics")
public class EduStatisticsDayController {

    @Resource
    private EduStatisticsDayService eduStatisticsDayService;


    @GetMapping("selectEduStatistics")
    public Result selectEduStatistics(String startTime, String endTime, String type){
        log.info(startTime);
        try{
            SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
            System.out.println(startTime+"-----");
            Date startDate =sdf.parse(startTime);
            Date endDate =sdf.parse(endTime);
            List<EduStatisticsDay> eduStatisticsDayList=this.eduStatisticsDayService.selectEduStatistics(startDate,endDate);
            List<String> showDate=new ArrayList<String>();  //统计js的X轴的日期显示
            List<Integer> data=new ArrayList<>(); //统计数据

            //把对应的时间存入集合中
            for(EduStatisticsDay s:eduStatisticsDayList){
                showDate.add(DateUtils.getCurrentYmd(s.getStatisticsTime()));
            }
            //分别存入集合
            for(int i=0 ; i<eduStatisticsDayList.size();i++){
                if(type.equals("registered")){//注册统计信息
                    data.add(eduStatisticsDayList.get(i).getRegisteredNum());
                }else if(type.equals("dailyUserNumber")){//每日用户数统计
                    data.add(eduStatisticsDayList.get(i).getDailyUserNumber());
                }else if(type.equals("dailyCourseNumber")){//每日课程数统计
                    data.add(eduStatisticsDayList.get(i).getDailyCourseNumber());
                }else if(type.equals("videoViewingNum")){//视频播放数
                    data.add(eduStatisticsDayList.get(i).getVideoViewingNum());
                }else{
                    //登录统计（活跃人数)
                    data.add(eduStatisticsDayList.get(i).getLoginNum());
                }
            }
            Map<String,Object> map=new HashMap<String,Object>();
            map.put("showDate",showDate);
            map.put("data",data);
            map.put("type",type);
            if(CollectionUtils.isEmpty(map)){
                return new Result(201,"无数据");
            }else{
                return new Result(200,"success",map);
            }

        }catch(Exception e){
            e.printStackTrace();
        }
        return null;
    }

    //查询7天内数据
    @GetMapping("getDaya")
    public Result getDaya(String type){
        List<EduStatisticsDay> list=this.eduStatisticsDayService.getDaya();
        Map<String,Object> map=this.getDays(list,type);
        if(CollectionUtils.isEmpty(map)){
            return new Result(401,"无数据");
        }else{
            return new Result(400,"success",map);
        }
    }

    //查询15天内数据
    @GetMapping("getDayb")
    public Result getDayb(String type){
        List<EduStatisticsDay> list=this.eduStatisticsDayService.getDayb();
        Map<String,Object> map=this.getDays(list,type);
        if(CollectionUtils.isEmpty(map)){
            return new Result(501,"无数据");
        }else{
            return new Result(500,"success",map);
        }
    }

    //查询30天内数据
    @GetMapping("getDayc")
    public Result getDayc(String type){
        List<EduStatisticsDay> list=this.eduStatisticsDayService.getDayc();
        Map<String,Object> map=this.getDays(list,type);
        if(CollectionUtils.isEmpty(map)){
            return new Result(301,"无数据");
        }else{
            return new Result(300,"success",map);
        }
    }


    //把数据存入map
    public Map<String,Object> getDays(List<EduStatisticsDay> list,String type){
        List<String> showDate=new ArrayList<String>();  //统计js的X轴的日期显示
        List<Integer> data=new ArrayList<>(); //统计数据

        for(EduStatisticsDay s: list){
            showDate.add(DateUtils.getCurrentYmd(s.getStatisticsTime()));
        }
        for(int i=0 ; i<list.size();i++){
            if(type.equals("registered")){//注册统计信息
                data.add(list.get(i).getRegisteredNum());
            }else if(type.equals("dailyUserNumber")){//每日用户数统计
                data.add(list.get(i).getDailyUserNumber());
            }else if(type.equals("dailyCourseNumber")){//每日课程数统计
                data.add(list.get(i).getDailyCourseNumber());
            }else if(type.equals("videoViewingNum")){//视频播放数
                data.add(list.get(i).getVideoViewingNum());
            }else{
                //登录统计（活跃人数
                data.add(list.get(i).getLoginNum());
            }


        }
        Map<String,Object> map=new HashMap<String,Object>();
        map.put("showDate",showDate);
        map.put("data",data);
        map.put("type",type);
        return map;
    }

}
